package com.example.webchar.webchar.service.serverimpl;

import com.example.webchar.webchar.mapper.master.ChapterNameMaster;
import com.example.webchar.webchar.mapper.master.NovelContentMaster;
import com.example.webchar.webchar.mapper.master.NovelMaster;
import com.example.webchar.webchar.mapper.slave.ChapterNameSlave;
import com.example.webchar.webchar.mapper.slave.NovelSlave;
import com.example.webchar.webchar.pojo.ChapterName;
import com.example.webchar.webchar.pojo.Novel;
import com.example.webchar.webchar.pojo.NovelContent;
import com.example.webchar.webchar.service.ChapteService;
import com.example.webchar.webchar.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: webchars
 * @description
 * @author: YouName
 * @create: 2021-08-28 08:31
 **/
@Component
@Slf4j
public class ChapteImpl implements ChapteService {
    @Autowired
    private ChapterNameSlave chapterNameSlave;
    @Autowired
    private ChapterNameMaster chapterNameMaster;
    @Autowired
    private NovelSlave novelSlave;
    @Autowired
    private NovelMaster novelMaster;
    @Autowired
    private NovelContentMaster novelContentMaster;
    @Autowired
    private RedisUtils redisUtils;
    private final static String REDIS_QUERY_CHAPTER = "queryChapte_";

    /**
     * 根据图书id查询对应的章节信息
     * @param novelId
     * @return
     */
    @Override
    public List<Novel> queryChapte(Integer novelId) {
        if (!redisUtils.hasKey(REDIS_QUERY_CHAPTER + novelId)) {
            log.info("走数据库");
            List<Novel> novels = novelSlave.quereyNovelById(novelId);
            //获取查询到的小说信息
            List<ChapterName> chapterNames = chapterNameSlave.quereChapter(novels);
            //尝试in的写法 查询到小说的章节信息
            for (Novel novel : novels) {
                ArrayList<ChapterName> chapterNames1 = new ArrayList<>();
                for (ChapterName chapterName : chapterNames) {
                    if (novel.getId().equals(chapterName.getNovelId())) {
                        chapterNames1.add(chapterName);
                    }
                }
                novel.setChapterNames(chapterNames1);
            }
            redisUtils.set(REDIS_QUERY_CHAPTER + novelId, novels);
            return novels;
        } else {
            log.info("走缓存");
            return (List<Novel>) redisUtils.get(REDIS_QUERY_CHAPTER + novelId);
        }


    }

    @Override
    public boolean deleteNovel(Integer novelId) {
        try {
            novelMaster.deleteNovelById(novelId);
            //根据id删除小说的信息
            List<ChapterName> chapterNames = chapterNameSlave.quereyChapterNameById(novelId);
            //查询小说的章节id
            chapterNameMaster.deleteChapterNameByNovelId(novelId);
            //删除小说的章节信息
            novelContentMaster.deleteNovelContentByChapterNameId(chapterNames);
            //删除小说的内容页面
            return true;
        } catch (Exception e) {
            return false;
        }


    }
}
